package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.news.EvaluationPersonInfo;
import com.ruoyi.system.domain.news.dto.EvaluationPersonInfoDto;
import com.ruoyi.system.domain.news.query.EvaluationPersonInfoQuery;
import com.ruoyi.system.domain.news.vo.EvaluationPersonInfoVo;
import com.ruoyi.system.service.news.EvaluationPersonInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/evaluationPersonInfo")
@Api(value = "evaluationPersonInfoController", tags  = "机构人员信息")
public class EvaluationPersonInfoController extends BaseController {

	@Resource
	private EvaluationPersonInfoService evaluationPersonInfoService;

    @ApiOperation(value = "新增/修改机构人员信息", notes = "新增/修改机构人员信息")
	@PostMapping("/submit")
	public R submit(@RequestBody EvaluationPersonInfoDto evaluationPersonInfoDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		EvaluationPersonInfo evaluationPersonInfo = new EvaluationPersonInfo();
		BeanUtils.copyProperties(evaluationPersonInfoDto,evaluationPersonInfo);
		if (evaluationPersonInfo.getId()==null) {
			evaluationPersonInfo.setCreateBy(loginUser.getUsername());
			evaluationPersonInfo.setDeptId(loginUser.getDeptId());
			evaluationPersonInfo.setCreateTime(new Date());
			evaluationPersonInfo.setUpdateBy(loginUser.getUsername());
			evaluationPersonInfo.setUpdateTime(new Date());
		} else {
			evaluationPersonInfo.setUpdateBy(loginUser.getUsername());
			evaluationPersonInfo.setUpdateTime(new Date());
		}
		boolean result = evaluationPersonInfoService.saveOrUpdate(evaluationPersonInfo);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除机构人员信息", notes = "删除机构人员信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = evaluationPersonInfoService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个机构人员信息", notes = "获取单个机构人员信息")
	@GetMapping("/getById")
	public R<EvaluationPersonInfoVo> getById(@RequestParam(value = "id") Long id) {
		EvaluationPersonInfoQuery evaluationPersonInfoQuery = new EvaluationPersonInfoQuery();
		evaluationPersonInfoQuery.setId(id);
		List<EvaluationPersonInfoVo> evaluationPersonInfoList = evaluationPersonInfoService.getEvaluationPersonInfoList(evaluationPersonInfoQuery);
		if (evaluationPersonInfoList!=null&&evaluationPersonInfoList.size()>0) {
			return R.ok(evaluationPersonInfoList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取机构人员信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<EvaluationPersonInfoVo>> list(@RequestBody EvaluationPersonInfoQuery evaluationPersonInfoQuery) {
		List<EvaluationPersonInfoVo> evaluationPersonInfoList = evaluationPersonInfoService.getEvaluationPersonInfoList(evaluationPersonInfoQuery);
		return R.ok(new PageInfo<>(evaluationPersonInfoList));
	}

	@ApiOperation(value = "导入机构人员模板下载")
	@PostMapping("/importTemplate")
	public void importTemplate(HttpServletResponse response)
	{
		ExcelUtil<EvaluationPersonInfo> util = new ExcelUtil<EvaluationPersonInfo>(EvaluationPersonInfo.class);
		util.importTemplateExcel(response, "机构人员数据");
	}

	@ApiImplicitParam(name = "agencyId", value = "机构ID", required = true, dataType = "int")
	@ApiOperation(value = "导入机构人员数据")
	@PostMapping("/importData")
	public R importData(MultipartFile file, String agencyId) throws Exception
	{
		ExcelUtil<EvaluationPersonInfo> util = new ExcelUtil<EvaluationPersonInfo>(EvaluationPersonInfo.class);
		List<EvaluationPersonInfo> evaluationPersonInfoList = util.importExcel(file.getInputStream());
		if (evaluationPersonInfoList.isEmpty()) {
			return R.fail("请填写数据");
		}
		for (EvaluationPersonInfo evaluationPersonInfo:evaluationPersonInfoList) {
			evaluationPersonInfo.setAgencyId(agencyId);
		}
		evaluationPersonInfoService.saveBatch(evaluationPersonInfoList);
		return R.ok("导入成功");
	}
}
